package com.channel.temperature.collect.app.bleupdate;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.inuker.bluetooth.library.beacon.Beacon;
import com.inuker.bluetooth.library.beacon.BeaconItem;
import com.inuker.bluetooth.library.beacon.BeaconParser;
import com.inuker.bluetooth.library.search.SearchResult;
import com.channel.temperature.collect.R;
import com.channel.temperature.collect.app.util.MyUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Created by dingjikerbo on 2016/9/1.
 */
public class DeviceListAdapter1 extends BaseAdapter implements Comparator<SearchResult> {

    private Context mContext;

    private List<SearchResult> mDataList;

    public DeviceListAdapter1(Context context) {
        mContext = context;
        mDataList = new ArrayList<SearchResult>();
    }

    public void setDataList(List<SearchResult> datas) {
        mDataList.clear();
        mDataList.addAll(datas);
        Collections.sort(mDataList, this);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mDataList.size();
    }

    @Override
    public Object getItem(int position) {
        return mDataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int compare(SearchResult lhs, SearchResult rhs) {
        return rhs.rssi - lhs.rssi;
    }

    private static class ViewHolder {
        TextView name;
        TextView mac;
        TextView rssi;
        TextView version;
        TextView isOpen;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;

        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.device_list_item1, null, false);

            holder = new ViewHolder();
            holder.name = (TextView) convertView.findViewById(R.id.name);
            holder.mac = (TextView) convertView.findViewById(R.id.mac);
            holder.rssi = (TextView) convertView.findViewById(R.id.rssi);
            holder.version = (TextView) convertView.findViewById(R.id.version);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final SearchResult result = (SearchResult) getItem(position);

        holder.name.setText(result.getName());
        holder.mac.setText(result.getAddress());
        holder.rssi.setText(String.format("Rssi: %d", result.rssi));
//        if (!result.getName().contains("ZNYL")) {
//            String version = getVersion(result);
//            holder.version.setText(version);
//        } else {
//            holder.version.setText("");
//        }


        Beacon beacon = new Beacon(result.scanRecord);
        BeaconItem extra = null;
        for (BeaconItem item : beacon.mItems) {
            if (item.type == 0XFF) {
                extra = item;
            }
        }

        if (extra != null) {
            BeaconParser beaconParser = new BeaconParser(extra);
            int firstByte = beaconParser.readByte(); // 读取第1个字节
            int secondByte = beaconParser.readByte(); // 读取第2个字节
            int lockstatus = beaconParser.readByte(); //读取第3个字节 锁状态指示

            boolean isDoorOpen = beaconParser.getBit(lockstatus, 4);  //获取第3字节的第4bit 门锁状态
            beaconParser.readShort();                     //电池信息  4,5
            beaconParser.readByte(); //按钮按下计时器   6    新东的是4
            int mode = ((beaconParser.readByte() >> 4) & 0xff);
            byte versionName = (byte) beaconParser.readByte();   //  锁类型  高4bit:当前程序模式    低4bit:锁类型 (3：蓝牙挂锁  )
//                        Log.e("测试", "测试 " + versionName + " " + String.format("V%2x", versionName));
//            holder.isOpen.setText(isDoorOpen ? "锁已打开" : "锁关闭");


            String dataString = MyUtil.bytesToHexString(extra.bytes);
            Log.e("dataString", "dataString : " + dataString);
            String versionCode = dataString.substring(14, 16);
            int versionCodeResult = 0;
            try {
                versionCodeResult = Integer.parseInt(versionCode);
            } catch (NumberFormatException e) {

            }
            holder.version.setText("V00" + versionCodeResult + "");
        }

        return convertView;
    }


    public String getVersion(SearchResult device) {
        Log.e("version", "version : " + device.getName());
        String version = "";
        if (device.getName() != null || !device.getName().equals("NULL")) {
            Beacon beacon = new Beacon(device.scanRecord);
            BeaconItem extra = null;

            for (BeaconItem item : beacon.mItems) {
                if (item.type == 0XFF) {
                    extra = item;
                }
            }
            if (extra != null) {
                BeaconParser beaconParser = new BeaconParser(extra);
                int firstByte = beaconParser.readByte(); // 读取第1个字节    0x52
                int secondByte = beaconParser.readByte(); // 读取第2个字节   0x54
                int lockstatus = beaconParser.readByte(); //读取第3个字节 锁状态指示

                beaconParser.getBit(lockstatus, 7); // 获取第3字节的第7bit    秘钥初始化否


                beaconParser.getBit(lockstatus, 6); // 获取第3字节的第6bit  电机解闭锁状态
                beaconParser.getBit(lockstatus, 5); //获取第3字节的第5bit 门磁状态
                beaconParser.getBit(lockstatus, 4);  //获取第3字节的第4bit 门锁状态
                beaconParser.readShort();                     //电池信息  4,5
                beaconParser.readByte(); //按钮按下计时器   6    新东的是4
                int mode = ((beaconParser.readByte() >> 4) & 0xff);
                byte versionName = (byte) beaconParser.readByte();   //  锁类型  高4bit:当前程序模式    低4bit:锁类型 (3：蓝牙挂锁  )

                version = String.format("V%2x", versionName);
            }
        } else {
            version = "";
        }
        return version;
    }

//    public String getZNYLVersion(SearchResult device) {
//        Log.e("version", "version : " + device.getName());
//        String version = "";
//        if (device.getName() != null||!device.getName().equals("NULL")) {
//            Beacon beacon = new Beacon(device.scanRecord);
//            BeaconItem extra = null;
//
//            for (BeaconItem item : beacon.mItems) {
//                if (item.type == 0XFF) {
//                    extra = item;
//                }
//            }
//            if (extra != null) {
//                BeaconParser beaconParser = new BeaconParser(extra);
//                int firstByte = beaconParser.readByte(); // 读取第1个字节    0x52
//                int secondByte = beaconParser.readByte(); // 读取第2个字节   0x54
//                int lockstatus = beaconParser.readByte(); //读取第3个字节 锁状态指示
//
//                beaconParser.getBit(lockstatus, 7); // 获取第3字节的第7bit    秘钥初始化否
//
//
//                beaconParser.getBit(lockstatus, 6); // 获取第3字节的第6bit  电机解闭锁状态
//                beaconParser.getBit(lockstatus, 5); //获取第3字节的第5bit 门磁状态
//                beaconParser.getBit(lockstatus, 4);  //获取第3字节的第4bit 门锁状态
//                beaconParser.readShort();                     //电池信息  4,5
//                beaconParser.readByte(); //按钮按下计时器   6    新东的是4
//                int mode = ((beaconParser.readByte() >> 4) & 0xff);
//                byte versionName = (byte) beaconParser.readByte();   //  锁类型  高4bit:当前程序模式    低4bit:锁类型 (3：蓝牙挂锁  )
//
//                version = String.format("V%2x", versionName);
//            }
//        } else {
//            version = "";
//        }
//        return version;
//    }
}
